/**
 * 菜单menu.js
 * 版本号： v1.0.0
 */
$(function(){
    var lastChecked = null;
    var rowCount = 0;
    $("#menu_dataList").treegrid({
        fitColumns: true,
        fit:true,
        method: "POST",
        rownumbers: true,
        singleSelect:true,
        border: false,
        url: CONTEXT_PATH + '/menu/getTreeMenu.do',
        idField: 'meId',
        striped:true,
        treeField: "name",
        frozenColumns:[[
            {field:'meId',checkbox:true}
        ]],
        columns: [
            [
                {field: 'name', title: '菜单名称', width: 220, fit: true},
                {field: 'href', title: '链接地址', width: 180, fit: true},
                {field: 'meType', title: '菜单类型', width: 70, fit: true, align: 'center', formatter:function(val){
                        if(val == 1) return "<span style='font-weight: bold'>页面</span>";
                        if(val == 2) return "<span style='font-weight: bold'>按钮</span>";
                 }},
                {field: 'sort', title: '排序', width: 70, align: 'center', fit: true},
                {field: 'isShow', title: '是否可见', width: 70, fit: true, align: 'center', formatter:function(val){
                    if(val == 1) return "<span style='color: green; font-weight: bold'>显示</span>";
                    if(val == 2) return "<span style='color: red; font-weight: bold'>隐藏</span>";
                }},
                {field: 'permission', title: '权限标识', fit: true, width: 260},
                {field: 'createDateFormat', title: '创建时间', width: 135, formatter:function(val){
                    return formatDatebox(val, "yyyy-MM-dd HH:mm:ss", true);
                }}
            ]
        ],
        // rowStyler: function(){
        //     rowCount++;
        //     if(rowCount % 2 == 0){
        //         return 'background-color:#c0c4f9;';
        //     }else{
        //         return 'background-color:#a8afff;';
        //     }
        // },
        onSelect:function(rowData){
        },
        onCheck:function(rowIndex, rowData){
            var row = $("#menu_dataList").treegrid("getChecked");
            if(row && rowIndex == lastChecked){
                $('#menu_dataList').treegrid("unselectAll");//取消选中当前所有行
                lastChecked = null;
            }else{
                lastChecked = rowIndex;
            }
        },
        onLoadSuccess:function(row, data){
            if(data.length > 0){
                lastChecked = null;
                $("#menu_dataList").treegrid("select", data[0].meId);
            }
        }
    });

    //定义可操作按钮
    $("#menu_menu").dialog({
        title: '',
        width: 'auto',
        height:35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });
});

/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for(var j = 0; j < btnOptions.length; j++ ){
        if(false != btnOptions[j].value){
            falg = true;
            break;
        }
    }
    if(!falg){
        //不存在按钮
        toolbar.push({});
    }else{
        //存在按钮
        for(var i = 0; i < btnOptions.length; i++){
            if(false != btnOptions[i].value){
                //添加按钮
                if("addBtn" == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            window.location.href = CONTEXT_PATH + "/menu/toMenuOption.do";
                        }
                    });
                }
                //修改按钮
                if("editBtn" == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            menu_edit();
                        }
                    });
                }
                //删除按钮
                if("delBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            menu_del();
                        }
                    });
                }
                //详情按钮
                if("viewBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            menu_view($(this).linkbutton("options").iconCls);
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

/**
 * 删除菜单
 */
function menu_del() {
    var row = $("#menu_dataList").treegrid("getSelected");
    var checked = $("#menu_dataList").treegrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({ title: '操作提示', msg: "请选择删除菜单", timeout: 3000, showType: "slide" });
        return;
    }
    if(row.children.length > 0){
        $.messager.show({ title: '操作提示', msg: "此菜单有子菜单，不能删除！", timeout: 3000, showType: "slide" });
        return;
    }
    $.messager.confirm('删除提示', '您确定要删除 ' + row.name + ' 菜单吗？', function (r) {
        if (r) {
            $.ajax({
                url: CONTEXT_PATH + "/menu/del.do?id=" + row.meId,
                type: "get",
                success: function (data) {
                    messager(data);
                    $("#menu_dataList").treegrid("reload", {});
                }
            })
        }
    });
}

/**
 * 修改菜单
 * @returns {boolean}
 */
function menu_edit(){
    var row = $("#menu_dataList").treegrid("getSelected");
    var checked = $("#menu_dataList").treegrid("getChecked");
    if (!checked || checked.length == 0) {
        $.messager.show({ title:'操作提示', msg:"请选择修改菜单", timeout:3000, showType:"slide" });
        return;
    }
    window.location.href = CONTEXT_PATH + "/menu/toMenuOption.do?id=" + row.meId;
}

/**
 * 刷新菜单
 */
function menu_view(iconCls){
    var row = $("#menu_dataList").datagrid("getSelected");
    if (row == null) {
        $.messager.show({ title: '操作提示', msg: "请选择需要查看的菜单详情", timeout: 3000, showType: "slide" });
        return;
    }
    //IFRAM打开窗口
    layoutSub(cdxq.id , cdxq.url + row.meId, iconCls, cdxq.closable, cdxq.title);
}
